﻿//=====================================================================================================
// LocalizationLibrary
// The Localization Library is a collection of reusable software components that provide 
// support for localization. This library enables you to localize WPF, Silverlight and WP7 applications.
// (http://localizationlibrary.codeplex.com)
//=====================================================================================================
// Author: Nicola Gallo (Website: www.nicola.gallo.name, Email: nicolagmt@hotmail.it) 
//=====================================================================================================

namespace System
{
    //.NET References
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;

    //LocalizationLibrary References
    using ReflectionStudio.LocalizationLibrary.Attributes;

    /// <summary>
    /// Defines a object's extension.
    /// </summary>
    public static class ObjectExtension
    {
        //Properties
        
        /// <summary>
        /// Raise the defined OnPropertyChange method for the input <see cref=""/>.
        /// </summary>
        /// <param name="source">Source.</param>
        /// <param name="propertyName">Property's name.</param>
        internal static void RaiseOnPropertyChangeMethod(this object source, String propertyName)
        {
            BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.FlattenHierarchy;
            MethodInfo[] methodInfos = source.GetType().GetMethods(bindingFlags);
            if (methodInfos != null)
            {
                foreach (MethodInfo methodInfo in methodInfos)
                {
                    Object[] customAttributes = 
                        methodInfo.GetCustomAttributes(typeof(OnPropertyChangeMethodAttribute), true);
                    if ((customAttributes != null) && (customAttributes.Length == 1))
                    {
                        OnPropertyChangeMethodAttribute attrib = (OnPropertyChangeMethodAttribute)customAttributes[0];
                        ParameterInfo[] parameterInfos = methodInfo.GetParameters();
                        if (parameterInfos != null)
                        {
                            Object[] parameters = new Object[parameterInfos.Length];
                            bool assigned = false;
                            int index = 0;
                            foreach (ParameterInfo parameterInfo in parameterInfos)
                            {
                                if ((parameterInfo.Name.Equals(attrib.ParameterName))||
                                    ((!assigned) && (String.IsNullOrEmpty(attrib.ParameterName)) &&
                                    (parameterInfo.ParameterType.IsAssignableFrom(typeof(String)))))
                                {
                                    parameters[index] = propertyName;
                                }
                                else 
                                {
                                    parameters[index] = parameterInfo.DefaultValue;
                                }
                                index++;
                            }
                            methodInfo.Invoke(source, parameters);
                        }
                        break;
                    }
                }
            }
        }
    }
}